﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using LinQ_DTO;
using System.Xml.Linq;
using System.Xml;

namespace TemplateH4.Design
{
    public partial class Design : System.Web.UI.Page
    {
        public String NameSave;
        public List<ImageAdd> aImageAdd;
        public TextAdd Text;
        public String srcSanPhamGoc;
        public String sizeSanPhamGoc = "S";
        public Guid MaNguoiDung;
        public int MaSanPhamThietKe = 0;
        protected void Page_PreInit(object sender, EventArgs e)
        {
            String thm;
            thm = (string)Session["themeSelected"];
            if (thm != null)
            {
                Page.Theme = thm;
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("~/config.xml"));
                XmlNode root = doc.DocumentElement;
                String theme = root.SelectSingleNode("theme").ChildNodes[0].Value.Trim();
                Session.Add("themeSelected", theme);
                Page.Theme = theme;
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
           
            MaNguoiDung = LayMaNguoiDung();
            MaSanPhamThietKe = LayMaSanPhamThietKe();
            //load Thiet ke
            if (Request.QueryString["DeleteTK"] != null)
            {
                int MaDelete = System.Convert.ToInt32(Request.QueryString["DeleteTK"]);
                if(MaDelete>0)
                    XoaThietKe(MaDelete);
                Response.Redirect("Design.aspx");
                return;
            }
            //Luu thiet ke
            System.Collections.Specialized.NameValueCollection
                    postedValues = Request.Form;
            String[] key;
            aImageAdd = new List<ImageAdd>();
            if (postedValues.AllKeys.Length == 0)
                return;
            ImageAdd temp = new ImageAdd();
            Text = new TextAdd();
            key = postedValues.GetValues(0);
            NameSave = key[0];

            key = postedValues.GetValues(1);
            srcSanPhamGoc = key[0];

            key = postedValues.GetValues(2);
            sizeSanPhamGoc = key[0];
            
            int index = 4;
            for(int i=0;i<4;i++)
            {
                key = postedValues.GetValues(index);
                temp.Status = System.Convert.ToInt32(key[0]);
                index++;

                key = postedValues.GetValues(index);
                temp.Src = key[0];
                index++;

                key = postedValues.GetValues(index);
                temp.Height = System.Convert.ToInt32(key[0]);
                index++;

                key = postedValues.GetValues(index);
                temp.Width = System.Convert.ToInt32(key[0]);
                index++;

                key = postedValues.GetValues(index);
                temp.X = System.Convert.ToInt32(key[0]);
                index++;

                key = postedValues.GetValues(index);
                temp.Y = System.Convert.ToInt32(key[0]);
                index++;
                ImageAdd tempAdd = new ImageAdd(temp);
                aImageAdd.Add(tempAdd);
            }
           
            key = postedValues.GetValues(index);
            Text.Status = System.Convert.ToInt32(key[0]);
            index++;

            key = postedValues.GetValues(index);
            Text.Text = key[0];
            index++;

            key = postedValues.GetValues(index);
            Text.Size = key[0];
            index++;

            key = postedValues.GetValues(index);
            Text.Color= key[0];
            index++;

            key = postedValues.GetValues(index);
            Text.X = System.Convert.ToInt32(key[0]);
            index++;

            key = postedValues.GetValues(index);
            Text.Y = System.Convert.ToInt32(key[0]);
            index++;
            if (MaSanPhamThietKe == 0)
            {
                int MaSPThem = LuuThietKe();
                //Them vao gio hang
                ThemVaoGioHang(MaSPThem);
                ///


                String strRedirect = "Design.aspx?MaTK=" + MaSPThem.ToString();
                Response.Redirect(strRedirect);
            }
            else
            {
                CapNhatThietKe();
                String strRedirect = "Design.aspx?MaTK=" + MaSanPhamThietKe.ToString();
                Response.Redirect(strRedirect);
            }
        }
        public String LaySrcTrangTri()
        {
            
            String srcTrangTri = "";
            TrangTriBUS tt = new TrangTriBUS();
            List<shop_trangtri> arrTT = tt.LayTatCaTrangTri();
            for (int i = 0; i < arrTT.Count; i++)
            {
                srcTrangTri += "\"" + arrTT.ElementAt(i).HinhAnh + "\"";
                if (i < arrTT.Count - 1)
                    srcTrangTri += ",";
            }
            return srcTrangTri;
        }
        public String LaySrcMauNam()
        {
            String SrcMau = "";
            SanPhamGocBUS tt = new SanPhamGocBUS();
            List<shop_sanphamgoc> arrMauNam = tt.LayTatCaSanPhamGocNam();
            for (int i = 0; i < arrMauNam.Count; i++)
            {
                SrcMau += "\"" + arrMauNam.ElementAt(i).HinhSanPhamGoc.Value + "\"";
                if (i < arrMauNam.Count - 1)
                    SrcMau += ",";
            }
            return SrcMau;
        }
        public String LaySrcMauNu()
        {
            String SrcMau = "";
            SanPhamGocBUS tt = new SanPhamGocBUS();
            List<shop_sanphamgoc> arrMauNu = tt.LayTatCaSanPhamGocNu();
            for (int i = 0; i < arrMauNu.Count; i++)
            {
                SrcMau += "\"" + arrMauNu.ElementAt(i).HinhSanPhamGoc.Value + "\"";
                if (i < arrMauNu.Count - 1)
                    SrcMau += ",";
            }
            return SrcMau;
        }
        public XElement ChiTietThietKe()
        {
            XElement result;
            XElement xmlSanPhamGoc = new XElement("SanPhamGoc",
                new XElement("src",srcSanPhamGoc),
                new XElement("size",sizeSanPhamGoc)
            );
            XElement xmlTrangTri1= new XElement("TrangTri1",
                new XElement("status", aImageAdd[0].Status.ToString()),
                new XElement("height", aImageAdd[0].Height.ToString()),
                new XElement("width", aImageAdd[0].Width.ToString()),
                new XElement("x", aImageAdd[0].X.ToString()),
                new XElement("y", aImageAdd[0].Y.ToString()),
                new XElement("src", aImageAdd[0].Src)
            );
            XElement xmlTrangTri2 = new XElement("TrangTri2",
                new XElement("status", aImageAdd[1].Status.ToString()),
                new XElement("height", aImageAdd[1].Height.ToString()),
                new XElement("width", aImageAdd[1].Width.ToString()),
                new XElement("x", aImageAdd[1].X.ToString()),
                new XElement("y", aImageAdd[1].Y.ToString()),
                new XElement("src", aImageAdd[1].Src)
            );
            XElement xmlTrangTri3 = new XElement("TrangTri3",
                new XElement("status", aImageAdd[2].Status.ToString()),
                new XElement("height", aImageAdd[2].Height.ToString()),
                new XElement("width", aImageAdd[2].Width.ToString()),
                new XElement("x", aImageAdd[2].X.ToString()),
                new XElement("y", aImageAdd[2].Y.ToString()),
                new XElement("src", aImageAdd[2].Src)
            );
            XElement xmlTrangTri4 = new XElement("TrangTri4",
                new XElement("status", aImageAdd[3].Status.ToString()),
                new XElement("height", aImageAdd[3].Height.ToString()),
                new XElement("width", aImageAdd[3].Width.ToString()),
                new XElement("x", aImageAdd[3].X.ToString()),
                new XElement("y", aImageAdd[3].Y.ToString()),
                new XElement("src", aImageAdd[3].Src)
            );
            XElement xmlText= new XElement("Text",
                new XElement("status", Text.Status.ToString()),
                new XElement("color", Text.Color),
                new XElement("size", Text.Size),
                new XElement("x", Text.X.ToString()),
                new XElement("y", Text.Y.ToString()),
                new XElement("text", Text.Text)
            );
            result = new XElement("Root", 
                xmlSanPhamGoc, 
                xmlTrangTri1, 
                xmlTrangTri2, 
                xmlTrangTri3, 
                xmlTrangTri4,
                xmlText
             );
            return result;
        }
        public Guid LayMaNguoiDung()
        {
            //kiem tra dang nhap
            if (Session["IsLogin"] == null || (int)Session["IsLogin"] == 0)
                return Guid.Empty; //test
            UserBUS user = new UserBUS();
            aspnet_User temp;
            temp = user.LayUserTheoTen((String)Session["UserName"]);
            return temp.UserId;
           // return Guid.Empty; //test
        }
        public int LayMaSanPhamThietKe()
        {
            if (Request.QueryString["MaTK"] != null)
                return System.Convert.ToInt32(Request.QueryString["MaTK"]);
            return 0; //test
        }
        public int LayMaSanPhamGoc()
        {
            SanPhamGocBUS sp = new SanPhamGocBUS();
            return sp.LayMaSanPhamGoc(srcSanPhamGoc); 
        }
        public int LuuThietKe()
        {
            shop_sanphamthietke ThietKeAdd = new shop_sanphamthietke();
            ThietKeAdd.ChiTiet = ChiTietThietKe();
            ThietKeAdd.MaNguoiDung = LayMaNguoiDung();
            ThietKeAdd.MaSanPhamGoc = LayMaSanPhamGoc();
            ThietKeAdd.NgayThietKe = DateTime.Now;
            SanPhamThietKeBUS sp = new SanPhamThietKeBUS();
            return sp.ThemSanPhamThietKe(ThietKeAdd);
        }
        public void CapNhatThietKe()
        {
            shop_sanphamthietke ThietKeAdd = new shop_sanphamthietke();
      //      ThietKeAdd.MaSanPhamThietKe = MaSanPhamThietKe;
            ThietKeAdd.ChiTiet = ChiTietThietKe();
            ThietKeAdd.MaNguoiDung = LayMaNguoiDung();
            ThietKeAdd.MaSanPhamGoc = LayMaSanPhamGoc();
            ThietKeAdd.NgayThietKe = DateTime.Now;
            SanPhamThietKeBUS sp = new SanPhamThietKeBUS();
            sp.CapNhatSanPhamThietKe(MaSanPhamThietKe,ThietKeAdd);
        }
        public String LoadThietKe()
        {
            
            String result="";
            SanPhamThietKeBUS sp = new SanPhamThietKeBUS();
            shop_sanphamthietke sptk = sp.LaySanPhamThietKeTheoMaSanPham(MaSanPhamThietKe);
            XElement xmlChiTiet = sptk.ChiTiet;
            XElement xmlSanPhamGoc = xmlChiTiet.Element("SanPhamGoc");
            XElement xmlTrangTri1 = xmlChiTiet.Element("TrangTri1");
            XElement xmlTrangTri2 = xmlChiTiet.Element("TrangTri2");
            XElement xmlTrangTri3 = xmlChiTiet.Element("TrangTri3");
            XElement xmlTrangTri4 = xmlChiTiet.Element("TrangTri4");
            XElement xmlText = xmlChiTiet.Element("Text");
            //SanPhamGoc
            srcSanPhamGoc = xmlSanPhamGoc.Element("src").Value;
            sizeSanPhamGoc = xmlSanPhamGoc.Element("size").Value;
            //TrangTri1
            aImageAdd = new List<ImageAdd>();
            ImageAdd temp = new ImageAdd();
            ImageAdd tempAdd;
            temp.Status = System.Convert.ToInt32(xmlTrangTri1.Element("status").Value);
            temp.Src = xmlTrangTri1.Element("src").Value;
            temp.Height = System.Convert.ToInt32(xmlTrangTri1.Element("height").Value);
            temp.Width = System.Convert.ToInt32(xmlTrangTri1.Element("width").Value);
            temp.X = System.Convert.ToInt32(xmlTrangTri1.Element("x").Value);
            temp.Y = System.Convert.ToInt32(xmlTrangTri1.Element("y").Value);
            tempAdd = new ImageAdd(temp);
            aImageAdd.Add(tempAdd);

            temp.Status = System.Convert.ToInt32(xmlTrangTri2.Element("status").Value);
            temp.Src = xmlTrangTri2.Element("src").Value;
            temp.Height = System.Convert.ToInt32(xmlTrangTri2.Element("height").Value);
            temp.Width = System.Convert.ToInt32(xmlTrangTri2.Element("width").Value);
            temp.X = System.Convert.ToInt32(xmlTrangTri2.Element("x").Value);
            temp.Y = System.Convert.ToInt32(xmlTrangTri2.Element("y").Value);
            tempAdd = new ImageAdd(temp);
            aImageAdd.Add(tempAdd);

            temp.Status = System.Convert.ToInt32(xmlTrangTri3.Element("status").Value);
            temp.Src = xmlTrangTri3.Element("src").Value;
            temp.Height = System.Convert.ToInt32(xmlTrangTri3.Element("height").Value);
            temp.Width = System.Convert.ToInt32(xmlTrangTri3.Element("width").Value);
            temp.X = System.Convert.ToInt32(xmlTrangTri3.Element("x").Value);
            temp.Y = System.Convert.ToInt32(xmlTrangTri3.Element("y").Value);
            tempAdd = new ImageAdd(temp);
            aImageAdd.Add(tempAdd);

            temp.Status = System.Convert.ToInt32(xmlTrangTri4.Element("status").Value);
            temp.Src = xmlTrangTri4.Element("src").Value;
            temp.Height = System.Convert.ToInt32(xmlTrangTri4.Element("height").Value);
            temp.Width = System.Convert.ToInt32(xmlTrangTri4.Element("width").Value);
            temp.X = System.Convert.ToInt32(xmlTrangTri4.Element("x").Value);
            temp.Y = System.Convert.ToInt32(xmlTrangTri4.Element("y").Value);
            tempAdd = new ImageAdd(temp);
            aImageAdd.Add(tempAdd);

            Text = new TextAdd();
            Text.Status = System.Convert.ToInt32(xmlText.Element("status").Value);
            Text.Text = xmlText.Element("text").Value;
            Text.Color = xmlText.Element("color").Value;
            Text.Size = xmlText.Element("size").Value;
            Text.X = System.Convert.ToInt32(xmlText.Element("x").Value);
            Text.Y = System.Convert.ToInt32(xmlText.Element("y").Value);
            result += "srcMauChinh =\"" + srcSanPhamGoc + "\";";
            for (int i = 0; i < 4; i++)
            {
                result += "arrTrangTri[" + i.ToString() + "].src =\"" + aImageAdd[i].Src + "\";";
                result += "arrTrangTri[" + i.ToString() + "].height = " + aImageAdd[i].Height.ToString() + ";";
                result += "arrTrangTri[" + i.ToString() + "].width = " + aImageAdd[i].Width.ToString() + ";";
                result += "arrTrangTri[" + i.ToString() + "].x = " + aImageAdd[i].X.ToString() + ";";
                result += "arrTrangTri[" + i.ToString() + "].y = " + aImageAdd[i].Y.ToString() + ";";
                result += "arrTrangTri[" + i.ToString() + "].status = " + aImageAdd[i].Status.ToString() + ";";
                
            }
            result += "textAdd.text = \"" + Text.Text + "\";";
            result += "textAdd.color = \"" + Text.Color + "\";";
            result += "textAdd.size =\"" + Text.Size + "\";";
            result += "textAdd.x = " + Text.X.ToString() + ";";
            result += "textAdd.y = " + Text.Y.ToString() + ";";
            result += "textAdd.status = " + Text.Status.ToString() + ";";

            return result;

        }
        public String LoadDanhSachThietKe()
        {
            String result="";
            SanPhamThietKeBUS sp = new SanPhamThietKeBUS();
            List<shop_sanphamthietke> lsSP;
            lsSP = sp.LaySanPhamThietKeTheoMaNguoiDung(MaNguoiDung);
            for (int i = 0; i < lsSP.Count; i++)
            {
                result += "\"" + lsSP[i].MaSanPhamThietKe + "\"";
                if (i < lsSP.Count - 1)
                    result += ",";
            }
            return result;
        }
        public void XoaThietKe(int MaTK)
        {
            SanPhamThietKeBUS sp = new SanPhamThietKeBUS();
            sp.XoaSanPhamThietKe(MaTK);
        }
        public void ThemVaoGioHang(int MaSanPhamThietKe)
        {
            shop_sanpham newSanPham = new shop_sanpham();
            newSanPham.TenSanPham = "Sản Phẩm Thiết Kế";
            newSanPham.Size = sizeSanPhamGoc;
            newSanPham.MaSanPham =  MaSanPhamThietKe;
            newSanPham.DonGia = 100000;  //test 
            newSanPham.MoTa = "Sản phẩm do người dùng tự thiết kế";
            newSanPham.SoLuongNhap = 1;
           // string xml = "<Hinh><HinhTruoc>~/" + srcSanPhamGoc + "</HinhTruoc> </Hinh>";
          //  XElement hinh = new XElement(xml);
            XElement hinh = new XElement("Hinh", new XElement("HinhTruoc","~/Design/"+srcSanPhamGoc));
            SanPhamGocBUS spgBUS = new SanPhamGocBUS();
            shop_sanphamgoc spg = spgBUS.LaySanPhamGoc(spgBUS.LayMaSanPhamGoc(srcSanPhamGoc));
            newSanPham.Phai = spg.Phai;
            newSanPham.HinhSanPham = hinh;

            List<shop_sanpham> GioHang = (List<shop_sanpham>)Session["GioHang"];
            if (GioHang == null)
            {    
                GioHang = new List<shop_sanpham>();
                GioHang.Add(newSanPham);
            }
            else
                GioHang.Add(newSanPham);
            Session["GioHang"] = GioHang;
        }


    }
}
